package cn.fws.meap.community.service.impl;

import java.util.Date;
import java.util.List;

import cn.fws.meap.community.entity.Article;
import cn.fws.meap.community.enums.ResourcesTypeEnum;
import cn.fws.meap.community.mapper.ProblemResourcesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.fws.framework.utils.ConvertUtils;
import cn.fws.framework.utils.StringUtils;
import cn.fws.framework.utils.UUIDUtils;
import cn.fws.meap.community.mapper.ResourcesMapper;
import cn.fws.meap.community.entity.Resources;
import cn.fws.meap.community.service.IResourcesService;

import org.springframework.transaction.annotation.Transactional;

/**
 * 资源 服务层实现
 *
 * @author chenkh
 * @date 2019-03-07
 */
@Service
@Transactional
public class ResourcesServiceImpl implements IResourcesService {

    @Autowired
    private ResourcesMapper resourcesMapper;
    @Autowired
    private ProblemResourcesMapper problemResourcesMapper;


    /**
     * 查询资源信息
     *
     * @param id 资源ID
     * @return 资源信息
     */
    @Override
    public Resources selectById(String id) {
        return resourcesMapper.selectById(id);
    }

    /**
     * 查询资源列表
     *
     * @param resources 资源信息
     * @return 资源集合
     */
    @Override
    public List<Resources> selectList(Resources resources) {
        return resourcesMapper.selectList(resources);
    }

    /**
     * 新增资源
     *
     * @param resources 资源信息
     * @return 结果
     */


    @Override

    public int insert(Resources resources) {
        //Resources资源对象设置主键
        resources.setId(UUIDUtils.getUUID());
        //Resources资源对象设置创建时间
        resources.setCreateTime(new Date());
        return resourcesMapper.insert(resources);
    }


    /**
     * 修改资源
     *
     * @param resources 资源信息
     * @return 结果
     */
    @Override
    public int update(Resources resources) {
        //设置修改时间
        resources.setUpdateTime(new java.util.Date());
        return resourcesMapper.update(resources);
    }

    /**
     * 删除资源对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteByIds(String ids) {
        return resourcesMapper.deleteByIds(ConvertUtils.toStrArray(ids));
    }

    /**
     * 封装Article对象
     *
     * @param article
     * @return
     */
    @Override
    public Article saveArticleResource(Article article) {
        //获取openID
        String openid = article.getOpenid();
        article.setArticleImg(saveOtMResource(article.getArticleImg(), openid,ResourcesTypeEnum.IMAGE.getCode()));
        article.setVideoId(saveOtMResource(article.getVideoId(), openid,ResourcesTypeEnum.VIDEO.getCode()));
        article.setVoiceId(saveOtMResource(article.getVoiceId(), openid,ResourcesTypeEnum.AUDIO.getCode()));
        return article;
    }

    /**
     * 根据资源地址查找资源id
     *
     * @return
     */
    @Override
    public String getAddressById(String resourcesId) {
        return resourcesMapper.getAddressById(resourcesId);
    }

    /**
     * 根据资源id查找资源地址
     *
     * @return
     */
    @Override
    public String getIdByAddress(String address) {
        return resourcesMapper.getIdByAddress(address);
    }

    /**
     * 批量将资源地址存储为资源对象并返回一组id
     *
     * @return
     */
    @Override
    public String saveOtMResource(String resUrl, String openid,String resType) {
        String imgIds = "";
        if (StringUtils.isNotEmpty(resUrl)) {
            //保存图片地址,获取图片ID
            String problemImg = resUrl;
            String[] imgList = problemImg.split(",");
            for (String imgAddress : imgList) {
                String imgId = addResources(imgAddress ,resType,openid);
                imgIds += imgId + ",";
            }
        }
        return imgIds;
    }


    /**
     * 根据文章id删除资源对象
     *
     * @return
     */
    @Override
    public void removeResources(String articleId) {
        problemResourcesMapper.deleteByArticleId(articleId);
    }

    /**
     * 检查新的资源地址与旧的资源地址是否相同，如果不存在则存储该资源对象
     *
     * @return
     */
    @Override
    public String updateMsgResources(String oldId, String openId, String newAddress, String resType){
        String msgId = oldId;
        if (StringUtils.isNotEmpty(oldId)) {
            String oldMsgAddress = getAddressById(oldId);
            if (!oldMsgAddress.equals(newAddress)) {
                msgId = addResources( newAddress, resType,openId);
            }
        }
        return msgId;
    }

    /**
     * 存入资源对象
     * @param problemMsgResource
     * @param meapDocument
     * @param openid
     * @return
     */

    @Override
    public String addResources(String problemMsgResource, String meapDocument, String openid) {
        Resources resources=new Resources();
        //Resources资源对象设置主键
        resources.setId(UUIDUtils.getUUID());
        resources.setResAddress(problemMsgResource);
        resources.setResType(meapDocument);
        resources.setOpenid(openid);
        resourcesMapper.insert(resources);
        return resources.getId();

    }

}